package com.goktech.config;

import com.goktech.interceptor.LoginInterceptor;
import com.goktech.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private RefreshTokenInterceptor refreshTokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // token 刷新拦截器
        registry.addInterceptor(refreshTokenInterceptor)
                .excludePathPatterns(
                        "/user/login",
                        "/user/register",
                        "/user/updPassCode",
                        "/user/imageGenerate",
                        "/swiper/allSwiper",
                        "/img/**",
                        "/commodity/**",
                        "/image/**"

                )
                .order(0);
        // 登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/user/me",
                        "/user/login",
                        "/user/register",
                        "/user/updPassCode",
                        "/user/imageGenerate",
                        "/swiper/allSwiper",
                        "/img/**",
                        "/commodity/**",
                        "/image/**"
                )
                .order(1);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
        定位用户头像地址
         */
        registry.addResourceHandler("img/consumerPic/**").addResourceLocations(
                "file:" +
                        System.getProperty("user.dir") +
                        System.getProperty("file.separator") + "img" +
                        System.getProperty("file.separator") + "consumerPic" +
                        System.getProperty("file.separator")
        );

        /*
        定位轮播图片地址
         */
        registry.addResourceHandler("img/swiperPic/**").addResourceLocations(
                "file:" +
                        System.getProperty("user.dir") +
                        System.getProperty("file.separator") + "img" +
                        System.getProperty("file.separator") + "swiperPic" +
                        System.getProperty("file.separator")
        );

        /*
        定位商品图片地址
         */
        registry.addResourceHandler("img/commodityPic/**").addResourceLocations(
                "file:" +
                        System.getProperty("user.dir") +
                        System.getProperty("file.separator") + "img" +
                        System.getProperty("file.separator") + "commodityPic" +
                        System.getProperty("file.separator")
        );

        /*
        定位商品详情图片地址
         */
        registry.addResourceHandler("img/image/**").addResourceLocations(
                "file:" +
                        System.getProperty("user.dir") +
                        System.getProperty("file.separator") + "img" +
                        System.getProperty("file.separator") + "image" +
                        System.getProperty("file.separator")
        );
    }
}
